﻿namespace MockingBird.Simulator.Framework.Configuration
{
    using System.Collections.Generic;
    using System.IO;
    using System.Xml;
    using MockingBird.Common.Infrastructure.Helpers;

    public class FolderManager : IFolderManager
    {
        private ServiceConfigurationSettings configSettings;
        private string dataFilesRoot;
        private string endpointsRoot;
        private string currentEndpointFolder;
        private string endpointConfigFolder;
        private string endpointResponseFolder;
        private string endpointRequestFolder;

        public static readonly string DataFilesFolderName = "dataFiles";
        public static readonly string EndpointFolderName = "endpoints";
        public static readonly string ConfigFolderName = "config";
        public static readonly string ResponsesFolderName = "responses";
        public static readonly string RequestsFolderName = "requests";
        public static readonly string MetadataFolderName = "metadata";
        public static readonly string LogsFolderName = "logs";
        private static readonly string configFileExtension = ".config";

        public FolderManager()
        {

        }


        public void CreateServiceFolders(ServiceConfigurationSettings targetConfigSettings)
        {
            this.configSettings = targetConfigSettings;
            this.InitDataFilesRoot(this.configSettings.SimulatorRoot);
            this.InitEndpointSpecificFolders(this.configSettings.EndpointName);

            this.CreateFolderTree();

        }

        public void InitDataFilesRoot(string basePath)
        {
            this.dataFilesRoot = Path.Combine(basePath, DataFilesFolderName);
            this.endpointsRoot = Path.Combine(this.dataFilesRoot, EndpointFolderName);

        }

        public string GetConfigRoot()
        {
            return FileHelper.CombinePaths(this.currentEndpointFolder, ConfigFolderName);
        }

        public string GetConfigRoot(string endpointName)
        {
            return FileHelper.CombinePaths(this.endpointsRoot, endpointName, ConfigFolderName);
        }

        public string GetConfigFile(string endpointName)
        {
            return FileHelper.CombinePaths(this.GetConfigRoot(endpointName), endpointName + configFileExtension);
        }


        public string GetResponseRoot(string endpointName)
        {
            return FileHelper.CombinePaths(this.dataFilesRoot, EndpointFolderName, endpointName, ResponsesFolderName);
        }

        public string GetRequestRoot(string endpointName)
        {
            return FileHelper.CombinePaths(this.dataFilesRoot, EndpointFolderName, endpointName, RequestsFolderName);
        }



        private void InitEndpointSpecificFolders(string endpointName)
        {

            this.currentEndpointFolder = Path.Combine(this.endpointsRoot, endpointName);
            this.endpointConfigFolder = Path.Combine(this.currentEndpointFolder, ConfigFolderName);
            this.endpointRequestFolder = Path.Combine(this.currentEndpointFolder, RequestsFolderName);
            this.endpointResponseFolder = Path.Combine(this.currentEndpointFolder, ResponsesFolderName);

        }



        private void CreateFolderTree()
        {

            // the user may have created a folder set already
            if (configSettings.CreateServiceFolders)
            {
                CreateSubfolder(this.dataFilesRoot, EndpointFolderName);
                CreateSubfolder(this.currentEndpointFolder, ConfigFolderName);
                CreateSubfolder(this.currentEndpointFolder, RequestsFolderName);
                CreateSubfolder(this.currentEndpointFolder, ResponsesFolderName);
                CreateSubfolder(this.currentEndpointFolder, MetadataFolderName);
                CreateSubfolder(this.currentEndpointFolder, LogsFolderName);
                CreateSubfolder(Path.Combine(this.currentEndpointFolder, LogsFolderName), RequestsFolderName);
                CreateSubfolder(Path.Combine(this.currentEndpointFolder, LogsFolderName), ResponsesFolderName);
            }
        }
        private static void CreateSubfolder(string baseFolder, string folderName)
        {
            if (!Directory.Exists(Path.Combine(baseFolder, folderName)))
            {
                Directory.CreateDirectory(Path.Combine(baseFolder, folderName));

            }
        }


        public void SaveRequestMessages(List<XmlDocument> messages)
        {
            foreach (XmlDocument msg in messages)
            {
                string fileName = Path.Combine(this.endpointRequestFolder, msg.DocumentElement.Name + ".xml");
                msg.Save(fileName);
            }

        }

        public void SaveResponseMessages(List<XmlDocument> messages)
        {
            foreach (XmlDocument msg in messages)
            {
                string fileName = Path.Combine(this.endpointResponseFolder, msg.DocumentElement.Name + ".xml");
                msg.Save(fileName);
            }


        }

    }
}
